package com.sctvcloud.bazhong.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ruihang.generalibrary.ui.dialog.BaseDialogFragment;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.SharedPreferencesUtil;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.application.Constances;
import com.sctvcloud.bazhong.beans.VoiceTemplateBean;
import com.sctvcloud.bazhong.ui.adapter.VoiceAdapter;

import java.util.ArrayList;
import java.util.List;

import static com.sctvcloud.bazhong.application.Constances.MID_FONT;
import static com.sctvcloud.bazhong.application.Constances.SMALL_FONT;


public class VoiceSetFragment extends BaseDialogFragment implements RadioGroup.OnCheckedChangeListener, View.OnClickListener, VoiceAdapter.OnItemBtnClick {

    public static final String PT_TEMPLATE = "vixy";//普通话模板
    public static final String SC_TEMPLATE = "xiaorong";//四川话模板

    private RadioGroup font_rg, voice_rg, template_rg;
    private RadioButton voice_speed_fast_rb, voice_speed_mid_rb, voice_speed_slow_rb;
    private RadioButton big_rb, mid_rb, small_rb;
    private RadioButton pt_rb, sc_rb;
    private CustomFontTextView preview_cv;
    private CustomFontTextView speedView;
    private RecyclerView voiceTemplateList;
    private CustomFontTextView templateView;
//    private CustomFontTextView voiceTemplate;

    private CustomFontTextView confirm;
    private VoiceAdapter adapter;
    private List<VoiceTemplateBean> templateList;
    private String template = "";
    private String speed = "0";
    private int fontSize = 0;

    private boolean isIniting = true;

    @Override
    protected int getContentRes() {
        return R.layout.dia_voice_set_fragment;
    }

    @Override
    protected void initViews(Dialog dialog, Bundle savedInstanceState) {
        voice_rg = (RadioGroup) dialog.findViewById(R.id.dia_voice_speed_rg);
        voice_speed_fast_rb = (RadioButton) dialog.findViewById(R.id.dia_voice_speed_big_rb);
        voice_speed_mid_rb = (RadioButton) dialog.findViewById(R.id.dia_voice_speed_mid_rb);
        voice_speed_slow_rb = (RadioButton) dialog.findViewById(R.id.dia_voice_speed_small_rb);
        speedView = (CustomFontTextView) dialog.findViewById(R.id.dia_voice_speed);
        confirm = (CustomFontTextView) dialog.findViewById(R.id.confirm);
        confirm.setOnClickListener(this);
//        voiceTemplate = (CustomFontTextView) dialog.findViewById(R.id.template);
        templateView = (CustomFontTextView) dialog.findViewById(R.id.voice_template);
        voiceTemplateList = (RecyclerView) dialog.findViewById(R.id.template_content_view);
        voiceTemplateList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        voiceTemplateList.setItemAnimator(new DefaultItemAnimator());
        adapter = new VoiceAdapter(getContext(), templateList, this);
        voiceTemplateList.setAdapter(adapter);
        voice_rg.setOnCheckedChangeListener(this);

        font_rg = (RadioGroup) dialog.findViewById(R.id.dia_font_choice_rg);
        big_rb = (RadioButton) dialog.findViewById(R.id.dia_font_big_rb);
        mid_rb = (RadioButton) dialog.findViewById(R.id.dia_font_mid_rb);
        small_rb = (RadioButton) dialog.findViewById(R.id.dia_font_small_rb);
        preview_cv = (CustomFontTextView) dialog.findViewById(R.id.dia_font_font_preview);
        preview_cv.setOnClickListener(this);
        font_rg.setOnCheckedChangeListener(this);

        template_rg = (RadioGroup) dialog.findViewById(R.id.dia_voice_template_rg);
        pt_rb = (RadioButton) dialog.findViewById(R.id.dia_voice_template_pt);
        sc_rb = (RadioButton) dialog.findViewById(R.id.dia_voice_template_sc);
        template_rg.setOnCheckedChangeListener(this);
        // 缓存字体大小
        int fontSize = (int) SharedPreferencesUtil.getParam(getActivity(), Constances.FONT_KEY, MID_FONT);
        if (Constances.BIG_FONT == fontSize) {
            big_rb.setChecked(true);
        } else if (Constances.MID_FONT == fontSize) {
            mid_rb.setChecked(true);
        } else if (Constances.SMALL_FONT == fontSize) {
            small_rb.setChecked(true);
        }

//        initData();
        // 缓存语音合成速度
        speed = (String) SharedPreferencesUtil.getParam(getActivity(), Constances.VOICE_SPEED_KEY, Constances.MID_SPEED);
        if (Constances.FAST_SPEED.equalsIgnoreCase(speed)) {
            voice_speed_fast_rb.setChecked(true);
        } else if (Constances.MID_SPEED.equalsIgnoreCase(speed)) {
            voice_speed_mid_rb.setChecked(true);
        } else if (Constances.SLOW_SPEED.equalsIgnoreCase(speed)) {
            voice_speed_slow_rb.setChecked(true);
        }
        template = (String) SharedPreferencesUtil.getParam(getActivity(), Constances.VOICE_TEMPLATE_KEY, PT_TEMPLATE);
        if (PT_TEMPLATE.equalsIgnoreCase(template)) {
            pt_rb.setChecked(true);
        } else {
            sc_rb.setChecked(true);
        }
//        for (int i = 0; i < templateList.size(); i++) {
//            if (templateList.get(i).getEnName().equalsIgnoreCase(template)) {
//                voiceTemplate.setText( templateList.get(i).getChName());
//                break;
//            }
//        }
        preview_cv.getPaint().setFakeBoldText(true);
        speedView.getPaint().setFakeBoldText(true);
        templateView.getPaint().setFakeBoldText(true);
    }

    private void initData() {
        templateList = new ArrayList<>();
        VoiceTemplateBean bean;
        bean = new VoiceTemplateBean("小燕—女青、中英、普通话", "xiaoyan");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小宇—男青、中英、普通话", "xiaoyu");
        templateList.add(bean);
//        bean = new VoiceTemplateBean("凯瑟琳—女青、英", "catherine");
//        templateList.add(bean);
//        bean = new VoiceTemplateBean("亨利—男青、英", "henry");
//        templateList.add(bean);
//        bean = new VoiceTemplateBean("玛丽—女青、英", "vimary");
//        templateList.add(bean);
        bean = new VoiceTemplateBean("小研—女青、中英、普通话", "vixy");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小琪—女青、中英、普通话", "xiaoqi");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小峰—男青、中英、普通话", "vixf");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小梅—女青、中英、粤语", "xiaomei");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小莉—女青、中英、台湾话", "xiaolin");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小蓉—女青、中、四川话", "xiaorong");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小芸—女青、中、东北话", "xiaoqian");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小坤—男青、中、河南话", "xiaokun");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小强—男青、中、湖南话", "xiaoqiang");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小莹—女青、中、陕西话", "vixying");
        templateList.add(bean);
        bean = new VoiceTemplateBean("小新—男童、中、普通话", "xiaoxin");
        templateList.add(bean);
        bean = new VoiceTemplateBean("楠楠—女童、中、普通话", "nannan");
        templateList.add(bean);
        bean = new VoiceTemplateBean("老孙—男老、中、普通话", "vils");
        templateList.add(bean);
        adapter.setDataList(templateList);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.confirm) {
            //缓存语音合成模板
            SharedPreferencesUtil.setParam(getActivity(), Constances.VOICE_TEMPLATE_KEY, template);
            // 缓存语音速度
            SharedPreferencesUtil.setParam(getActivity(), Constances.VOICE_SPEED_KEY, speed);
            // 缓存字体大小
            SharedPreferencesUtil.setParam(getActivity(), Constances.FONT_KEY, fontSize);
            if (callBack != null) {
                Message message = new Message();
                message.what = fontSize;
                callBack.onCallBackMsg(this, message);
            }
            this.dismissAllowingStateLoss();
        }
    }

    @Override
    public void onItemBtnClick(VoiceTemplateBean templateBean) {
//        voiceTemplate.setText(templateBean.getChName());
        template = templateBean.getEnName();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.dia_voice_speed_big_rb:
                choiceVoice(voice_speed_fast_rb, voice_speed_mid_rb, voice_speed_slow_rb, Constances.FAST_SPEED);
                break;
            case R.id.dia_voice_speed_mid_rb:
                choiceVoice(voice_speed_mid_rb, voice_speed_fast_rb, voice_speed_slow_rb, Constances.MID_SPEED);
                break;
            case R.id.dia_voice_speed_small_rb:
                choiceVoice(voice_speed_slow_rb, voice_speed_fast_rb, voice_speed_mid_rb, Constances.SLOW_SPEED);
                break;
            case R.id.dia_font_big_rb:
                choiceFont(big_rb, mid_rb, small_rb, Constances.BIG_FONT);
//                preview_cv.setTextSize(Constances.BIG_FONT);
                break;
            case R.id.dia_font_mid_rb:
                choiceFont(mid_rb, big_rb, small_rb, MID_FONT);
//                preview_cv.setTextSize(MID_FONT);
                break;
            case R.id.dia_font_small_rb:
                choiceFont(small_rb, big_rb, mid_rb, SMALL_FONT);
//                preview_cv.setTextSize(SMALL_FONT);
                break;
            case R.id.dia_voice_template_pt:
                choiceTemplate(pt_rb, sc_rb, PT_TEMPLATE);
//                preview_cv.setTextSize(SMALL_FONT);
                break;
            case R.id.dia_voice_template_sc:
                choiceTemplate(sc_rb, pt_rb, SC_TEMPLATE);
//                preview_cv.setTextSize(SMALL_FONT);
                break;
        }
    }

    private void choiceVoice(RadioButton rb1, RadioButton rb2, RadioButton rb3, String speed) {
        rb1.setBackgroundResource(R.drawable.choice_bg);
        rb1.setTextColor(getResources().getColor(R.color.white));
        rb2.setBackgroundResource(R.drawable.choice_no_bg);
        rb2.setTextColor(getResources().getColor(R.color.colorGray98));
        rb3.setBackgroundResource(R.drawable.choice_no_bg);
        rb3.setTextColor(getResources().getColor(R.color.colorGray98));
        this.speed = speed;
    }

    private void choiceFont(RadioButton rb1, RadioButton rb2, RadioButton rb3, int size) {
        rb1.setBackgroundResource(R.drawable.choice_bg);
        rb1.setTextColor(getResources().getColor(R.color.white));
        rb2.setBackgroundResource(R.drawable.choice_no_bg);
        rb2.setTextColor(getResources().getColor(R.color.colorGray98));
        rb3.setBackgroundResource(R.drawable.choice_no_bg);
        rb3.setTextColor(getResources().getColor(R.color.colorGray98));

        fontSize = size;

//        if (isIniting) {
//            isIniting = false;
//            return;
//        }
//        if (callBack != null) {
//            Message message = new Message();
//            message.what = size;
//            callBack.onCallBackMsg(this, message);
//        }
    }

    private void choiceTemplate(RadioButton rb1, RadioButton rb2, String template) {
        rb1.setBackgroundResource(R.drawable.choice_bg);
        rb1.setTextColor(getResources().getColor(R.color.white));
        rb2.setBackgroundResource(R.drawable.choice_no_bg);
        rb2.setTextColor(getResources().getColor(R.color.colorGray98));
        this.template = template;
    }
}
